<template>
  <el-row class="login-container">
    <el-col :lg="16" :md="12" class="left-container">
      <div>
        <div class="left-top">欢迎光临</div>
        <div class="text-light-50 text-sm">
          此节点是（vue3 + vite实战商城后天开）视频课的演示地址
        </div>
      </div>
    </el-col>

    <el-col :lg="8" :md="12" class="right-container">
      <h2 class="font-bold text-3xl text-gray-800">欢迎回来</h2>
      <div class="right-login-title">
        <span class="h-[1px] w-16 bg-gray-200"></span>
        <span>账号密码登录</span>
        <span class="h-[1px] w-16 bg-gray-200"></span>
      </div>
      <el-form
        :model="form"
        class="w-[250px]"
        :rules="rules"
        ref="loginFormRef"
      >
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            class="w-[250px]"
            color="#626aef"
            round
            @click="submit"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
  import { reactive, ref, onMounted, onUnmounted } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElNotification } from 'element-plus'
  import { useCookies } from '@vueuse/integrations/useCookies'
  import { setToken, toast } from '@/utils'
  import { useStore } from 'vuex'
  import { useLoginKeyUp } from '@/hooks/useLoginKeyUp'

  const router = useRouter()
  const store = useStore()
  const cookie = useCookies()
  const loading = ref(false)
  const loginFormRef = ref(null)
  const form = reactive({
    username: '',
    password: '',
  })
  const rules = {
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [
      {
        message: '请输入密码',
        trigger: 'blur',
        min: 3,
        max: 10,
      },
    ],
  }

  function submit() {
    loginFormRef.value.validate((valid, fields) => {
      if (!valid) {
        return false
      } else {
        loading.value = true
        store
          .dispatch('loginAction', form)
          .then((res) => {
            toast('登录成功')
            router.push('/')
          })
          .finally(() => {
            loading.value = false
          })
      }
    })
  }

  useLoginKeyUp(submit)
</script>

<style scoped>
  .login-container {
    @apply bg-indigo-500 min-h-screen;
  }
  .left-container {
    @apply flex items-center justify-center flex-col;
  }
  .left-top {
    @apply font-bold text-5xl text-light-50 mb-4;
  }

  .right-container {
    @apply bg-light-50 flex items-center justify-center flex-col;
  }
  .right-login-title {
    @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
  }

  :deep(.el-input__suffix) {
    cursor: pointer;
  }
</style>
